const teamArray = require('../models/teamTable');
const teamTable = teamArray[0];
const teamReplyTable=teamArray[1];
const inspirecloud = require('@byteinspire/api');
const ObjectId = inspirecloud.db.ObjectId;

/**
 * TeamService
 * Service 是业务具体实现，由 Controller 或其它 Service 调用
 * 包含待办事项的增删改查功能
 */
class TeamService {
  /**
   * 通过名称查询队伍信息
   */
  async listAll(ctx) {
    return await teamTable.where().find();
  }

  /**
   * 查询队伍信息
   */
   async get(userid) {
    const teamList = await teamTable.where({createBy:ObjectId(userid)}).find();
    return teamList;
  }

  /**
 * 创建队伍
 */
  async add(teamInfo) {
    teamInfo.createBy = ObjectId(teamInfo.createBy);
    teamInfo.members=[];
    return await teamTable.save(teamInfo);
  }

  /**
* 修改队伍信息
*/
  async edit(editInfo) {
    const teamId = editInfo.teamId;
    let teamItem = await teamTable.where({_id: ObjectId(teamId)}).findOne();

    for(const key in editInfo){
      if(editInfo[key]!=null && key!="createBy"){
        teamItem[key] = editInfo[key];
      }
    }
    if(teamItem.createBy.toString()==editInfo.createBy){
      await teamTable.save(teamItem);
      return true;
    }

    return false;

  }

  /**
* 删除队伍信息
*/
  async del(teamid) {
    const result =  await teamTable.where({_id:ObjectId(teamid)}).delete();
    return result.deletedCount>=1;
  }

  /**
  * 查询回复信息
  */
  async getReply(teamid) {
    const teamReplyList = await teamReplyTable.where({teamId:ObjectId(teamid)}).find();
    return teamReplyList;
  }

  /**
  * 创建信息回复
  */
  async addReply(replyInfo) {
    replyInfo.createdBy = ObjectId(replyInfo.createdBy);
    replyInfo.teamId = ObjectId(replyInfo.teamId);
    return await teamReplyTable.save(replyInfo);
  }

  /**
  * 删除信息回复
  */
  async delReply(replyID) {
    const result =  await teamReplyTable.where({_id:ObjectId(replyID)}).delete();
    return result.deletedCount>=1;
  }

  /**
  * 修改信息回复
  */
  async editReply(editInfo) {
    const replyId = editInfo.teamreplyId;
    let replyItem = await teamReplyTable.where({_id: ObjectId(replyId)}).findOne();

    if(editInfo.content!=null && editInfo.createdBy==replyItem.createdBy.toString()){
      replyItem.content = editInfo.content;
      await teamReplyTable.save(replyItem);
      return true;
    }
    else{
      return false;
    }
    
   
  }

  /**
  * 加入队伍
  */
  async join(info) {
    const teamid = ObjectId(info.teamId);
    const userid = info.userId;
    let teamItem = await teamTable.where({_id: teamid}).findOne();
    if(teamItem===null) return "Team Not Found";

    if(!teamItem.members.includes(userid)){
      teamItem.members.push(userid);
      await teamTable.save(teamItem);
      return true;
    }else{
      return false;
    }
  }

  /**
  * 退出队伍
  */
  async quit(info) {
    const teamid = ObjectId(info.teamId);
    const userid = info.userId;
    let teamItem = await teamTable.where({_id: teamid}).findOne();
    const index = teamItem.members.indexOf(userid);

    let result=true;
    if(index>=0) teamItem.members.splice(index,1);
    else result = false;

    await teamTable.save(teamItem);
    return result;
  }
}

// 导出 Service 的实例
module.exports = new TeamService();
